#!/bin/bash
########################################################
# File Name: install_php.sh
# Author: Henson
# Created Time: 2017-02-26
########################################################
#clear
. /etc/init.d/functions

green="\033[32;1m"
red="\033[31;1m"
blue="\033[34;1m"
purple="\033[35;1m"
end="\033[0m"

DIR=/apps/src/php

Install_lib(){
    yum -y install gcc gcc-c++ bison bison-devel zlib zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel perl-DBI php-gd php-mysql php-bcmath php-mbstring php-xml pcre pcre-devel  openssl openssl-devel gd-devel libgcrypt-devel libxslt-devel libgpg-error-devel glib2-devel e2fsprogs-devel

}

Install_php(){
    cd $DIR
    if [ ! -f php-5.6.20.tar.gz ];then
        echo "php-5.6.20.tar.gz no exists!"
        exit
    fi

    #groupadd nginx
    #useradd -r -g nginx -s /sbin/nologin nginx

    tar -xf php-5.6.20.tar.gz
    #echo -e "${green}php开始编译，请稍后！${end}"
    cd php-5.6.20
    ./configure \
    --prefix=/usr/local/php5 \
    --with-fpm-user=nginx --with-fpm-group=nginx \
    --with-config-file-path=/usr/local/php5/etc \
    --enable-inline-optimization \
    --disable-debug \
    --disable-rpath \
    --enable-shared \
    --enable-opcache \
    --enable-fastcgi \
    --enable-fpm \
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-gettext \
    --enable-mbstring \
    --with-iconv \
    --with-mcrypt \
    --with-mhash \
    --with-openssl \
    --enable-bcmath \
    --enable-soap \
    --with-libxml-dir \
    --enable-pcntl \
    --enable-shmop \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-sockets \
    --with-curl \
    --with-zlib \
    --enable-zip \
    --with-bz2 \
    --with-readline \
    --with-gd \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-xsl \
    --with-config-file-path=/usr/local/lib
    #make && make install
    make -j $(awk '{if($1=="processor"){i++}}END{print i}' /proc/cpuinfo) && make install

    \cp php.ini-production /usr/local/php5/etc/php.ini
    \cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    chkconfig --add php-fpm
    chkconfig php-fpm on
    \cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
    \cp $DIR/php-fpm.conf /usr/local/php5/etc
    /etc/init.d/php-fpm start
    #chmod 777 /dev/shm/php-fcgi.sock
    #echo
    #echo -e "${green}PHP安装完成！${end}"
    #sleep 2
}

main(){
    Install_lib
    Install_php
}
main